from utils.id_utils import IdUtils
from utils.time_utils import TimeUtils
from utils.sql_utils import SQLManager
from flask import jsonify

SLIDER_FONT_SUFFIX = 'slider'
NOTE_FONT_SUFFIX = 'note'

class ImgService:
    def insert_slider_path(self, upload_path):
        """
        插入幻灯片图片路径到数据库
        :param upload_path: 文件上传路径
        :param note_id: 笔记 ID
        :return: 插入后的记录 ID 或错误信息
        """
        # if not upload_path or not note_id:
        #     raise ValueError("Invalid parameters provided")
        
        try:
            # 生成所需的参数
            unique_id = IdUtils.generate_id()
            current_time = TimeUtils.get_current_time()
            
            # SQL 查询语句
            sql = """
                INSERT INTO rb_note_slide_imgs
                (unique_id, slide_img_url, is_deleted,
                  modify_time, created_at)
                VALUES
                (%s, %s, %s,%s, %s)
            """
            # 参数列表
            params = (unique_id, upload_path,0,'', current_time,)
            
            # 执行 SQL
            SQLManager().insert(sql, params)
            return {"unique_id": unique_id}
        except Exception as e:
            return False
    
    # 文章主图片
    def insert_note_index_path(self, upload_path, unique_id):
        try:
            # SQL 查询语句
            sql = """
                UPDATE rb_note_content
                SET image_url = %s
                WHERE unique_id = %s
            """
            # 参数列表
            params = (upload_path,unique_id,)
            
            return SQLManager().insert(sql, params)
        except Exception as e:
            return False
    
    def update_slider_img_info_by_note_id(note_id,unique_id):
        print('update_img_info:param',note_id,unique_id)
        sql = """
            UPDATE rb_note_slide_imgs
            SET note_id = %s
            WHERE unique_id = %s
        """

        values=(note_id,unique_id)
        return SQLManager().insert(sql, values)
    

    def update_note_index_img_path(note_id,image_url):
        print('update_img_info:param',note_id,image_url)
        sql = """
            UPDATE rb_note_content
            SET image_url = %s
            WHERE unique_id = %s
        """

        values=(image_url,note_id)
        return SQLManager().insert(sql, values)
    
    def delete_slider_photo_by_unique_id(unique_id):
        sql = """
            UPDATE rb_note_slide_imgs
            SET is_deleted = 1
            WHERE unique_id = %s
        """

        values=(unique_id)
        return SQLManager().insert(sql, values)
    
    def update_slider_by_note_id(slide_img_url,note_id):
        sql = """
            UPDATE rb_note_slide_imgs
            SET slide_img_url = %s
            WHERE note_id = %s
        """

        values=(slide_img_url,note_id)
        return SQLManager().insert(sql, values)
    
    def query_note_slider_by_note_id(note_id,current,page_size):
        sql = """
            SELECT
                rnsi.unique_id AS slider_id,
                rnsi.note_id AS note_id,
                rnsi.slide_img_url AS image_url
            FROM
                rb_note_slide_imgs rnsi
            WHERE
               rnsi.note_id = %s
            AND
               rnsi.is_deleted = 0;
        """
        current = current or 1
        page_size = page_size or 5
        data = SQLManager().get_list(sql,args=(note_id))  # 获取分页数据
        return data